package com.woniuxy.ticketcinema.controller;


import com.woniuxy.ticketcinema.entity.Cinema;
import com.woniuxy.ticketcinema.exception.UpdatValidationException;
import com.woniuxy.ticketcinema.service.CinemaService;
import com.woniuxy.ticketcommon.entity.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yanqiwei
 * @since 2021-02-20
 */
@RestController
@RequestMapping("/cinema")
public class CinemaController {

    @Autowired
     private CinemaService cinemaService;

    @PostMapping("/add")
    public ResponseResult addCinema(@Validated @RequestBody Cinema cinema){

        boolean save = cinemaService.save(cinema);

        return save ? new ResponseResult().setCode(200).setMsg("添加成功") : new ResponseResult().setCode(500).setMsg("添加失败");
    }

    @GetMapping("/getAllCinemaInfo")
    public ResponseResult getAllCinemaInfo(){
        List<Cinema> cinemaList = cinemaService.list();
        return new ResponseResult().setCode(200).setMsg("查询成功").setData(cinemaList);
    }

    @PostMapping("/getCinemaInfoById")
    public ResponseResult getCinemaInfoById(@Valid int id){
        Cinema cinema = cinemaService.getById(id);
        return new ResponseResult().setCode(200).setMsg("查询成功").setData(cinema);
    }

    @PostMapping("/updateCinemaInfoById")
    public ResponseResult updateCinemaInfoById(@Validated(UpdatValidationException.class) @RequestBody Cinema cinema){
        cinemaService.updateById(cinema);
        return new ResponseResult().setCode(200).setMsg("修改成功");
    }
}

